/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import blueprint4j.db.DBConnection;


/**
 * This will offer select options from a select in the database
 */
public class BindStringSelectDB extends BindString implements BindStringSelectInterface {

	private String sql_select = null,display_field=null,options_field=null;
	private List<String> options = new ArrayList<String>(),display = new ArrayList<String>();
	private DBConnection dbcon = null;

	/**
	 * Build a sql select
	 * The display values will come from 
	 */
	public BindStringSelectDB( String p_name, String p_description,String sql_select,String display_field,String options_field,DBConnection dbcon,Bindable entity ) {
		super( p_name,p_description,entity );
		this.sql_select = sql_select;
		this.display_field =display_field;
		this.options_field = options_field;
		this.dbcon = dbcon;
	}

	/**
	 * This build the sql select and populates the values
	 */
	public void refresh()
	throws BindException {
		try {
			Statement smt = dbcon.createStatement();
			options = new ArrayList<String>();
			display = new ArrayList<String>();
			for (ResultSet rs = smt.executeQuery(sql_select);rs.next();) {
				options.add(rs.getString(options_field));
				display.add(rs.getString(display_field));
			}
			smt.close();
		} catch (Exception exception) {
			throw new BindException(exception);
		}
	}

	public void set(String value){
		if (value != null && !options.contains(value)) {
			Log.debug.out("BindStringSelectDB set to a value ["+value+"] which is not conatined in the options list ["+options+"]","");
		}
		setObject(value);
	}

	public String[] getOptions() {
		return options.toArray(new String[0]);
	}

	public String[] getDisplays() {
		return display.toArray(new String[0]);
	}

	public String getDisplay() {
		if (get() != null && display.size() > options.indexOf(get())) {
			return display.get(options.indexOf(get()));
		}
		return null;
	}

}
